#include "Server.h"
#include <QCoreApplication>
#include <iostream>
#include <QDir>

int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    std::cout << "Starting server..." << std::endl;
    QString processId = (argc > 1) ? QString(argv[1]) : "socketHandPro23043";
//    QString path = (argc > 2) ? QString(argv[2]) : "/Users/zhanghongwei/Desktop/socket-temp/socket-project/res/track";
    QString path = (argc > 2) ? QString(argv[2]) : "/data/app/xth/socket-temp/socket-project/res/track";
    if (processId.isEmpty()) {
        std::cerr << "Process ID is empty" << std::endl;
        return -1;
    }

    QString prefix = "socketHandPro";
    pid_t pidA = 0;
    if (processId.startsWith(prefix)) {
        QString pidString = processId.mid(prefix.length());
        bool ok;
        pidA = pidString.toInt(&ok);
        if (!ok) {
            std::cerr << "Failed to convert PID from string" << std::endl;
            return -1;
        }
        std::cout << "Extracted PID: " << pidA << std::endl;
    } else {
        std::cerr << "Process ID does not start with the expected prefix" << std::endl;
        return -1;
    }
//    QString appDir = QCoreApplication::applicationDirPath();
//    QString absolutePythonPath = QDir(appDir).absoluteFilePath("../python");
    QString absolutePythonPath = path;
    std::cout << "Absolute Python Path: " << absolutePythonPath.toStdString() << std::endl;

    Server server;
    server.initializePython(absolutePythonPath.toStdString());

    server.startServer(processId);

    return app.exec();
}
